package com.jy.rhin.facade.infra.controller.document;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.application.infra.DocumentTemplateEditApplicationService;
import com.jy.rhin.facade.infra.controller.dto.CDADocumentTemplateDto;
import com.jy.rhin.facade.infra.controller.dto.CDADocumentTemplateNodeDto;
import com.jy.rhin.facade.infra.controller.dto.ImportCDADocumentTemplateReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/infra/documentTemplateEdit")
@Api(tags = "文档模板编辑api")
public class DocumentTemplateEditController {

    @Resource
    DocumentTemplateEditApplicationService documentTemplateEditApplicationService;

    @GetMapping("/cda/page")
    @ApiOperation("获取分页的cda模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "模板编号/名称", required = false),
            @ApiImplicitParam(name = "current", value = "页码", required = false),
            @ApiImplicitParam(name = "size", value = "每页多少条", required = false)
    })
    public Result<Page<CDADocumentTemplateDto>> pageCDADocumentTemplate(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "current", defaultValue = "1", required = false) Integer current,
            @RequestParam(value = "size", defaultValue = "10", required = false) Integer size) {
        return Result.success(documentTemplateEditApplicationService.pageCDADocumentTemplate(key, current, size));
    }

    @PostMapping("/cda/import")
    @ApiOperation(value = "导入cda模板文件")
    public Result<CDADocumentTemplateNodeDto> importCDADocumentTemplate(@Validated @RequestBody ImportCDADocumentTemplateReq req) throws Exception {
        return Result.success(documentTemplateEditApplicationService.importCDADocumentTemplate(req));
    }

    @PostMapping("/cda/previewByNodes")
    @ApiOperation(value = "通过节点配置预览cda模板文件")
    public Result<String> previewCDADocumentTemplateByNodes(@RequestBody CDADocumentTemplateNodeDto req) throws Exception {
        return Result.success(documentTemplateEditApplicationService.previewCDADocumentTemplateByNodes(req));
    }

    @PostMapping("/cda")
    @ApiOperation(value = "保存cda模板文件")
    public Result saveCDADocumentTemplate(@Validated @RequestBody CDADocumentTemplateDto req) throws Exception {
        documentTemplateEditApplicationService.saveCDADocumentTemplate(req);
        return Result.success();
    }

    @GetMapping("/cda")
    @ApiOperation(value = "获取cda模板文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    public Result<CDADocumentTemplateDto> getCDADocumentTemplate(@RequestParam(value = "id", required = true) Long id) {
        return Result.success(documentTemplateEditApplicationService.getCDADocumentTemplate(id));
    }

    @PostMapping("/cda/delete")
    @ApiOperation(value = "删除cda模板文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    public Result deleteCDADocumentTemplate(@RequestParam(value = "id", required = true) Long id) {
        documentTemplateEditApplicationService.deleteCDADocumentTemplate(id);
        return Result.success();
    }

    @GetMapping("/cda/preview")
    @ApiOperation(value = "预览cda模板文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    public Result<String> previewCDADocumentTemplate(@RequestParam(value = "id", required = true) Long id) throws Exception {
        return Result.success(documentTemplateEditApplicationService.previewCDADocumentTemplate(id));
    }

    @GetMapping("/cda/download")
    @ApiOperation(value = "下载cda模板文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true)
    })
    public void downloadCDADocumentTemplate(@RequestParam(value = "id", required = true) Long id, HttpServletResponse response) throws Exception {
        documentTemplateEditApplicationService.downloadCDADocumentTemplate(id, response);
    }

}
